import * as THREE from 'three'
import { EventBus } from '../utils/EventBus'
// 灯光类
export class MyLight {
  constructor(scene) {
    this.scene = scene
    this.nowSpotLight = {} //聚光灯光源对象
    this.nowSceneName = '展厅'
    // 平行光位置列表
    this.directionPositionList = [
      [0, 5, 10],
      [-10, 5, 0],
      [0, 5, 10],
      [10, 5, 0]
    ]
    this.createCarDirectionLight()
    this.createSpotLight()

    EventBus.getInstance().on('changeSky', (sceneName) => {
      if (sceneName === this.nowSceneName) return
      if (sceneName === '展厅') {
        // 创建聚光灯
        this.createSpotLight()
      } else if (sceneName === '户外') {
        // 删除聚光灯
        this.removeSpotLight()
      }
      this.nowSceneName = sceneName
    })
  }
  // 创建平行光
  createCarDirectionLight() {
    this.directionPositionList.forEach(dp => {
      const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5)
      directionalLight.position.set(...dp)
      this.scene.add(directionalLight)
    })
  }
  // 创建聚光灯
  createSpotLight() {
    this.nowSpotLight = new THREE.SpotLight(0xffffff, 1)
    this.nowSpotLight.angle = 0.16 * Math.PI// 聚光灯角度
    this.nowSpotLight.penumbra = 0.5// 光的衰减程度
    this.nowSpotLight.castShadow = true// 开启阴影效果
    this.nowSpotLight.position.set(0, 5, 0)
    this.nowSpotLight.shadow.mapSize.set(4096, 4096)// 阴影的宽高
    this.scene.add(this.nowSpotLight)
  }
  // 删除聚光灯
  removeSpotLight() {
    this.nowSpotLight.parent.remove(this.nowSpotLight)
    this.nowSpotLight = {}
  }
}